/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.infrastructure.mysql;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.itbox.example.domain.model.UserModel;
import cn.itbox.example.domain.repository.UserRepository;
import cn.itbox.example.infrastructure.mysql.entity.UserDO;
import cn.itbox.example.infrastructure.mysql.dao.UserDao;
import cn.itbox.uap.db.model.PageResult;
import cn.itbox.uap.domain.util.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.infrastructure.repository
 * @Description TODO
 * @ClassName UserRepositoryImpl
 * @Author xingc
 * @Date 2023/11/26 18:53
 * @Version 1.0.0
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class UserRepositoryImpl implements UserRepository {
    private final UserDao userDao;

    @Override
    public UserModel byId(Long id) {
        UserDO userDO = userDao.selectById(id);
        return BeanUtil.copyProperties(userDO, UserModel.class);
    }

    @Override
    public void delete(Long id) {
        userDao.deleteById(id);
    }

    @Override
    public UserModel save(UserModel userModel) {
        UserDO userDO = BeanUtil.copyProperties(userModel, UserDO.class);
        if (null == userDO.getId()) {
            userDao.insert(userDO);
            userModel.setId(userDO.getId());
        } else {
            userDao.updateById(userDO);
        }

        return BeanUtil.copyProperties(userDO, UserModel.class);
    }

    @Override
    public List<UserModel> findAll() {
        List<UserDO> userDOS = userDao.selectList(new QueryWrapper<>());
        return userDOS.stream().map(item -> BeanUtil.copyProperties(item, UserModel.class)).collect(Collectors.toList());
    }

    @Override
    public PageResult<UserModel> findPage(Long pageIndex, Long pageSize) {
        Page<UserDO> page = new Page<UserDO>().setCurrent(pageIndex).setSize(pageSize);
        Page<UserDO> userDOPage = userDao.selectPage(page, new QueryWrapper<>());
        return Converter.mpPage2PageResult(userDOPage, UserModel.class);
    }

    @Override
    public PageResult<UserModel> findPage(String userName, Long pageIndex, Long pageSize) {
        Page<UserDO> page = new Page<UserDO>().setCurrent(pageIndex).setSize(pageSize);
        Page<UserDO> userDOPage = userDao.selectPage(page, new LambdaQueryWrapper<UserDO>().like(UserDO::getUserName, userName));

        return Converter.mpPage2PageResult(userDOPage, UserModel.class);
    }
}
